const common = require("./webpack.common");
const webpack = require("webpack");
const { merge } = require("webpack-merge"); //按需加载
const CopyWebpackPlugin = require("copy-webpack-plugin");
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin"); //内置的js压缩插件
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = merge(common, {
	mode: "production",
	output: {
		filename: "[name].[contenthash:8].bundle.js",
	},
	optimization: {
		minimize: true, //压缩代码 去掉未引用代码
		usedExports: true, //只导出外部使用了的成员  未引用成员字体变淡
		concatenateModules: true, //合并函数 也叫 scope hoisting 作用域提升
		minimizer: [
			//自定义压缩器插件
			new OptimizeCssAssetsWebpackPlugin(), //css压缩器
			new TerserWebpackPlugin(), //js压缩器
		],
	},
	plugins: [
		new CopyWebpackPlugin({
			//拷贝静态资源到输出目录，对开发环境开销比较大
			// patterns: [
			//6.0以上的用法
			// {
			// 	from: "public", //源文件
			// 	to: ".", //输出文件名称
			// },
			// ],
			patterns: ["public/favicon.ico"],
		}),
		new CleanWebpackPlugin(),
		new webpack.DefinePlugin({ API_BASE_URL: '"http://me.api/"' }),
	],
});
